package handler;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

public class DragView extends View {

	// Number of pixels to add to the dragged item for scaling. Should be even
	// for pixel alignment.
	private static final int DRAG_SCALE = 0; // In Launcher, value is 40

	private Bitmap _bitmap;
	private Paint _paint;
	private int _registrationX;
	private int _registrationY;

	private float _scale;
	private float _animationScale = 1.0f;

	private WindowManager.LayoutParams _layoutParams;
	private WindowManager _windowManager;

	public DragView(Context context, Bitmap b, int registrationX,
			int registrationY, int left, int top, int width, int height) {
		// TODO Auto-generated constructor stub
		super(context);

		_windowManager = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);

		Matrix v_scale = new Matrix();
		float v_scaleFactor = width;
		v_scaleFactor = _scale = (v_scaleFactor + DRAG_SCALE) / v_scaleFactor;
		v_scale.setScale(v_scaleFactor, v_scaleFactor);
		_bitmap = Bitmap.createBitmap(_bitmap, left, top, width, height,
				v_scale, true);

		_registrationX = registrationX + (DRAG_SCALE / 2);
		_registrationX = registrationY + (DRAG_SCALE / 2);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		setMeasuredDimension(_bitmap.getWidth(), _bitmap.getHeight());
	}

	@Override
	protected void onDraw(Canvas canvas) {
		if (true) {
			// for debugging
			Paint p = new Paint();
			p.setStyle(Paint.Style.FILL);
			p.setColor(0x88dd0011);
			canvas.drawRect(0, 0, getWidth(), getHeight(), p);
		}
		float v_scale = _animationScale;
		if (v_scale < 0.999f) {
			float v_width = _bitmap.getWidth();
			float v_offset = (v_width - (v_width * v_scale)) / 2;
			canvas.translate(v_offset, v_offset);
			canvas.scale(v_scale, v_scale);
		}

		canvas.drawBitmap(_bitmap, 0.0f, 0.0f, _paint);
	}

	@Override
	protected void onDetachedFromWindow() {
		super.onDetachedFromWindow();
		_bitmap.recycle();
	}

	public void setPaint(Paint paint) {
		_paint = paint;
		invalidate();
	}

	public void setScale(float scale) {
		if (scale > 1.0f)
			_animationScale = 1.0f;
		else
			_animationScale = scale;
		invalidate();
	}

	public void remove() {
		_windowManager.removeView(this);
	}

	public void move(int touchX, int touchY) {
		WindowManager.LayoutParams v_lp = _layoutParams;
		v_lp.x = touchX - _registrationX;
		v_lp.y = touchY - _registrationY;
		_windowManager.updateViewLayout(this, v_lp);
	}

	public void show(IBinder windowToken, int touchX, int touchY) {
		WindowManager.LayoutParams v_lp;
		int v_pxFormat;
		
		v_pxFormat = PixelFormat.TRANSLUCENT;
		v_lp = new WindowManager.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                touchX-_registrationX, touchY-_registrationY,
                WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL,
                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
                    | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
                    /*| WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM*/,
                v_pxFormat);
//        lp.token = mStatusBarView.getWindowToken();
        v_lp.gravity = Gravity.LEFT | Gravity.TOP;
        v_lp.token = windowToken;
        v_lp.setTitle("DragView");
        _layoutParams = v_lp;

        _windowManager.addView(this, v_lp);

	}

}
